package cn.edu.ujn.controller;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.dao.Customer;
import cn.edu.ujn.dao.User;
import cn.edu.ujn.service.ICustomerService;
import cn.edu.ujn.service.IUserService;
@Controller
public class CustomerController {
	@Autowired
	private ICustomerService customerService;
	@Autowired
	private IUserService userService;
	
	@RequestMapping(value = "lect")
	public String selectCustomer(Customer customer,Model model) {
		customer  = customerService.selectCustomer(customer.getCustId());
		model.addAttribute("cus",customer);
		return "customer";
		
	}
	
	@RequestMapping(value = "/customer/insert")
	public String addCustomer(Customer customer) {
		int add  = customerService.addCustomer(customer);
		return "customeradd";
	}
	
	@RequestMapping(value = "/chazhao")
	public String ChaZhao() {
		return "customerdisplay";
	}
	
	@RequestMapping(value = "/zengjia")
	public String ZengJia() {
		return "customerinsert2";
	}

	@RequestMapping(value = "selectdisplay")
	public String SelectDisplay() {
		return "customer";
    }
	
	@RequestMapping(value = "/customerdisplay")
	public String Display() {
		return "customerdisplay";
    }
	
	@RequestMapping(value ="/customer/find")
	 public String findCustomerByType(HttpSession session, Integer custId, String custName,String custAddress,String custPhone,String custIndustry,String custSource, Model model) {
		model.addAttribute("custId", custId);
		model.addAttribute("custName", custName);
		model.addAttribute("custAddress", custAddress);
		model.addAttribute("custPhone", custPhone);
		model.addAttribute("custIndustry", custIndustry);
		model.addAttribute("custSource", custSource);
		 List<Customer> findCustomerByType = customerService.findCustomerByType(custId, custName, custAddress, custIndustry, custSource);
		 model.addAttribute("findCustomerByType",findCustomerByType);
		 return "customer";

	 }
	
	
	@RequestMapping(value ="/customer/delete")
	 @ResponseBody
	 public String deleteCustomer( Integer id) {
		 int deleteCustomer = customerService.deleteCustomer(id);
		 if(deleteCustomer > 0){			
		        return "OK";
		    }else{
		        return "FAIL";			
		    }
	 }
	
	@RequestMapping(value ="/customer/update2")
	 public String updateCustomer2(Customer customer) {
		 int updateCustomer = customerService.updateCustomer(customer);
		 System.out.println(customer);
		 return "customer";
	 }
	
	@RequestMapping(value ="/customer/update/{id}")
	 public String updateCustomer(@PathVariable Integer id,Model model) {
		Customer customer = customerService.selectCustomer(id);
		 model.addAttribute("cust",customer);
		 return "customerinsert";
	 }
}
